*******************************************************************************
*** Description: 	This document provides the code for preparing the	 	***
***					Comparative Study of Electoral Systems (CSES)			***
***					Integrated Module Dataset (IMD) data for use in the		*** 
***					replication of "Compulsory Voting Diminishes the		***
***					Relationship Between Winning and Satisfaction with 		***
***					Democracy," which is authored by Shane P. Singh and		***
***					and appears in the Journal of Politics.					***
*******************************************************************************

**************
**************
*Set the Version                                                                                                                         
**************
**************
version 17.0



**************
**************
*Open the CSES IMD Data                                                                                                                         
**************
**************
use "CSES_IMD_JOP.dta", clear


**************
**************
*Create Variable That Identifies Respondents
**************
**************
gen respondent = IMD1005

**************
**************
*Create Variables That Identify Country-Year Surveys 
**************
**************
gen cntryyear = IMD1004
replace cntryyear = "DEU_2002" if IMD1004 == "DEU12002" //*create single identifier for Germany 2002 mailback and phone portions
replace cntryyear = "DEU_2002" if IMD1004 == "DEU22002" //*create single identifier for Germany 2002 mailback and phone portions

gen country_name = IMD1006_NAM
replace country_name = "United Kingdom" if country_name == "Great Britain"
replace country_name = "South Korea" if country_name == "Republic of Korea"
replace country_name = "Russia" if country_name == "Russian Federation"
replace country_name = "United States" if country_name == "United States of America"

tostring IMD1008_YEAR, gen(year_string) 
gen countryandyear = country_name+"_"+year_string
gen countryandyear_no_underscore = country_name+" "+year_string
drop year_string

encode cntryyear, gen(cntryyearnum)
label var cntryyearnum "numerical identifier for election"


**************
**************
*Create the Democratic Development Varible Using Data from the Polity IV Project Included in the CSES IMD and the Freedom House
**************
**************
gen  dem_development = IMD5051_1
replace dem_development = . if IMD5051_1 == -66  //*replace interruption periods as missing
replace dem_development = . if IMD5051_1 == -77  //*replace interregum periods as missing
replace dem_development = . if IMD5051_1 == -88  //*replace transition periods as missing
replace dem_development = . if IMD5051_1 ==  99	 //*replace 99 coding for missing data as missing 

*fill in missing data for Peru, sourced from Polity website
replace dem_development = 5 if cntryyear == "PER_2000" 
replace dem_development = 9 if cntryyear == "PER_2011"

*use Freedom House scores to approximate Polity scores for Hong Kong and Iceland and put them on the same -10 to 10 scale as Polity,
*with higher values meaning more freedom. Freedom House scores from: https://freedomhouse.org/ 
replace dem_development =  1 	if cntryyear == "ISL_1999" 
replace dem_development =  1 	if cntryyear == "ISL_2003" 
replace dem_development =  1 	if cntryyear == "ISL_2007" 
replace dem_development =  1 	if cntryyear == "ISL_2009" 
replace dem_development =  1 	if cntryyear == "ISL_2013" 


replace dem_development =  (((7-dem_development)/6)*20)-10 	if cntryyear == "ISL_1999" 
replace dem_development =  (((7-dem_development)/6)*20)-10 	if cntryyear == "ISL_2003" 
replace dem_development =  (((7-dem_development)/6)*20)-10 	if cntryyear == "ISL_2007" 
replace dem_development =  (((7-dem_development)/6)*20)-10 	if cntryyear == "ISL_2009" 
replace dem_development =  (((7-dem_development)/6)*20)-10 	if cntryyear == "ISL_2013" 

label var dem_development "democratic development"


**************
**************
*Create the Majoritarian Electoral System Variable
**************
**************
gen majoritarian = 0
label var majoritarian "1 if majoritarian electoral system for lower house, 0 otherwise"
replace majoritarian = 1 if IMD5013 == 1


**************
**************
*Create the Latin America Dummy
**************
**************
gen latin = 0
label var latin "country speaks Span., French, or Port. and is in the Americas"
replace latin = 1 if country_name == "Argentina"
replace latin = 1 if country_name == "Bolivia"
replace latin = 1 if country_name == "Brazil"
replace latin = 1 if country_name == "Chile"
replace latin = 1 if country_name == "Colombia"
replace latin = 1 if country_name == "Costa Rica"
replace latin = 1 if country_name == "Dominican Republic"
replace latin = 1 if country_name == "Ecuador"
replace latin = 1 if country_name == "El Salvador"
replace latin = 1 if country_name == "Guatemala"
replace latin = 1 if country_name == "Haiti"
replace latin = 1 if country_name == "Honduras"
replace latin = 1 if country_name == "Mexico"
replace latin = 1 if country_name == "Nicaragua"
replace latin = 1 if country_name == "Panama"
replace latin = 1 if country_name == "Paraguay"
replace latin = 1 if country_name == "Peru"
replace latin = 1 if country_name == "Uruguay"
replace latin = 1 if country_name == "Venezuela"



**************
**************
*Bring in the Turnout Measure Create the Compulsory Voting Variable
**************
**************
preserve
use "V-Dem_JOP_Replication.dta", clear
collapse v2elcomvot v2elvaptrn, by(countryandyear)
rename v2elcomvot v2elcomvot_v11
save "CV_turnout_v11_VoD.dta", replace
restore


merge m:1 countryandyear using "CV_turnout_v11_VoD.dta", ///
	keepusing(v2elcomvot_v11 v2elvaptrn)
	
*fill in turnout info based on VoD coding for country-years not in VoD using CSES info
replace v2elvaptrn  =  IMD5006_2 if cntryyear == "PHL_2004" //* fill in missing V-Dem value with CSES data
label var 	v2elvaptrn "turnout as percent of voting age population, from V-Dem"
 
	
*fill in compulsory voting info based on VoD coding for country-years not in VoD
replace v2elcomvot_v11 = 0 if country_name == "Hong Kong"
replace v2elcomvot_v11 = 0 if cntryyear == "KOR_2000"
replace v2elcomvot_v11 = 0 if cntryyear == "KOR_2004"

gen comp_enforced_VoD_v11 = 0
replace comp_enforced_VoD_v11 =  1 if v2elcomvot_v11>1
replace comp_enforced_VoD_v11 =  . if v2elcomvot_v11==.
label var comp_enforced_VoD_v11 "1 if enforced CV; 0 otherwise"

drop if _merge==2
drop _merge
drop v2elcomvot_v11



**************
**************
*Create the Parliamentary Election Dummy
**************
**************
gen parliamentary_election = 0
replace parliamentary_election = 1 if IMD1009 == 10
replace parliamentary_election = . if IMD1009 == .
label var parliamentary_election "1 if parliamentary election, 0 otherwise"



**************
**************
*Clean Up and Order Variables
**************
**************
drop IMD1004 - IMD5051_1 

order respondent cntryyear country_name countryandyear countryandyear_no_underscore cntryyearnum voted_winner_PM_PREZ voted_winner_GOV_PREZ satdem  female educ_scale_0_10 income age ageCV ideo matters_who_vote_for matters_who_is_in_power  mean_dev_from_LR_ind mean_dev_from_LR_exp comp_enforced_VoD_v11 v2elvaptrn majoritarian dem_development latin parliamentary_election


**************
************** 
*Save the Data
**************
**************  
save "CSES_IMD_JOP_Replication.dta", replace
